<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <!-- 自定义搜索框 -->
      <el-form :inline="true" :model="searchForm" class="mb-2 search_self">
        <el-form-item label="工单号">
          <el-input v-model="searchForm.docNo" placeholder="请输入工单号" @keyup.enter.native="handleSearch" />
        </el-form-item>
        <el-button @click="handleSearch">搜索</el-button>
      </el-form>
      <avue-crud ref="crud" :data="tableData" :permission="permissionList" :table-loading="tableLoading"
        :option="tableOption" @on-load="" @row-click="handleRowClick" @search-change="searchChange"
        @refresh-change="refreshChange" @size-change="sizeChange" @current-change="currentChange"
        @row-update="handleUpdate" @row-save="handleSave" @row-del="handleDel">
        <template slot="menuLeft">
          <el-button v-if="permissions.workorder_workorderproductionreport_export" class="filter-item" plain
            type="primary" size="small" icon="el-icon-download" @click="exportExcel">导出
          </el-button>
        </template>
      </avue-crud>
      <div :style="{ width: '100%', height: '1px', background: '#EBEEF5' }"></div>
      <el-tabs v-model="summaryActive" type="card" @tab-click="handleClick"
        style="margin-top: 5px;height: 70vh;border: 1px solid #EBEEF5;">
        <el-tab-pane label="工序信息" name="processInfo">
          <processInfo class="avue_self" ref="processInfo" :lineMainId="lineId" :processId="processId" :itemCode="itemCode" />
        </el-tab-pane>
        <el-tab-pane label="检测数据" name="checkData">
          <measureDR8 class="avue_self" v-if="processName && processName.includes('测量') && measureFlg === 'DR8'" :docNo="docNo"
            :workshopName="workshopName" :realityLineName="realityLineName" :itemCode="itemCode" ref="dr8" />
          <measureGEN class="avue_self" v-if="processName && processName.includes('测量') && measureFlg === 'GEN'" :docNo="docNo"
            :workshopName="workshopName" :realityLineName="realityLineName" :itemCode="itemCode" ref="gen" />
          <mtPro class="avue_self" v-if="processName && processName.includes('干涉')" :docNo="docNo" :workshopName="workshopName"
            :realityLineName="realityLineName" :itemCode="itemCode" ref="mtPro" />
          <polarity class="avue_self" v-if="processName && processName.includes('极性')" :docNo="docNo" :workshopName="workshopName"
            :realityLineName="realityLineName" :itemCode="itemCode" ref="polarity" />
          <opltSHHS class="avue_self" v-if="processName && processName.includes('插回损')" :docNo="docNo" :workshopName="workshopName"
            :realityLineName="realityLineName" :itemCode="itemCode" ref="opltSHHS" />
          <el-empty class="avue_self" v-else description="当前工序暂无检测数据"></el-empty>
        </el-tab-pane>
        <el-tab-pane label="报工数据" name="reportData">
          <reportData class="avue_self" ref="reportData" :docNo="docNo" :workshopName="workshopName" :productLineName="realityLineName"
            :itemCode="itemCode" :processId="processId" />
        </el-tab-pane>
        <el-tab-pane label="不良明细" name="badDetails">
          <badDetails class="avue_self" ref="badDetails" :docNo="docNo" :workshopName="workshopName" :realityLineName="realityLineName"
            :itemCode="itemCode" :processId="processId" />
        </el-tab-pane>
        <el-tab-pane label="报废明细" name="scrapDetails">
          <scrapDetails class="avue_self" ref="scrapDetails" :docNo="docNo" :workshopName="workshopName"
            :realityLineName="realityLineName" :itemCode="itemCode" :processId="processId" />
        </el-tab-pane>
        <el-tab-pane label="物料信息" name="bomInfo">
          <bomInfo class="avue_self" :docNo="docNo" ref="bomInfo" />
        </el-tab-pane>
      </el-tabs>

    </basic-container>
  </div>
</template>

<script>
import { getListData, addObj, putObj, delObj } from '@/api/workorder/workorderproductionreport'
import { remote } from "@/api/admin/dict";
import { tableOption } from '@/const/crud/workorder/workorderproductionreport'
import { mapGetters } from 'vuex'
import processInfo from "@/views/workorder/workorderproductionreport/processInfo.vue";
import measureDR8 from '@/views/workorder/measuredr8/index';
import mtPro from '@/views/workorder/interferometermtpro/summary';
import polarity from '@/views/workorder/polarityproduction/index';
import opltSHHS from '@/views/workorder/opltshhs/summary';
import measureGEN from '@/views/workorder/measuregen/index';
import reportData from "@/views/workorder/workorderproductionreport/reportData.vue";
import bomInfo from "@/views/workorder/workorderproductionreport/bomInfo.vue";
import badDetails from "@/views/workorder/workorderreportdetails/index.vue";
import scrapDetails from "@/views/workorder/workorderreportscrapdetails/index.vue";

export default {
  name: 'workorderproductionreport',
  components: {
    processInfo,
    bomInfo,
    reportData,
    measureDR8,
    mtPro,
    polarity,
    opltSHHS,
    measureGEN,
    badDetails,
    scrapDetails
  },
  data() {
    return {
      searchForm: {
        docNo: ''
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      summaryActive: 'processInfo',
      lineId: null,//产线ID
      processId: null,//工序ID
      workstation: null,//工位
      processName: null,//工序名称
      docNo: null,//工单号
      workshopName: null,//车间名称
      realityLineName: null,//实际产线名称
      itemCode: null,//品号
      docNoTypeList: [],
      measureFlg: null,//测量标志
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.workorder_workorderproductionreport_add, false),
        delBtn: this.vaildData(this.permissions.workorder_workorderproductionreport_del, false),
        editBtn: this.vaildData(this.permissions.workorder_workorderproductionreport_edit, false)
      }
    }
  },
  created() {
    this.getDocNoType();
    if (this.$route.query.docNo != null && this.$route.query.docNo != '') {
      this.searchForm.docNo = this.$route.query.docNo;
      this.getList(this.page);
    }
  },
  methods: {
    getDocNoType() {
      remote("product_line").then(response => {
        this.docNoTypeList = response.data.data;
      })
    },
    // 列表查询
    getList(page, params) {
      this.tableLoading = true
      if (!this.validatenull(this.searchForm.docNo)) {
        getListData(this.searchForm).then(response => {
          this.tableData = response.data.data
          this.handleClick("refresh");
          this.tableLoading = false
        }).catch(() => {
          this.tableLoading = false
        })
      } else {
        console.log("3333", this.searchForm);
        //提示输入工单号
        this.$message.warning('请输入工单号')
        this.tableLoading = false
      }
    },
    handleSearch() {
      this.loading = true
      if (this.searchForm.docNo == '' || this.searchForm.docNo == null) {
        this.$message.warning('请输入工单号')
      }
      this.getList();
    },
    // 删除
    handleDel: function (row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/workorder/workorderproductionreport/export', this.searchForm, 'workorderproductionreport.xlsx')
    },
    handleClick(tab) {
      if (this.validatenull(this.docNo) && tab !== "refresh") {
        this.$message.warning('请选择要查询的工单数据')
        return;
      }
      if (this.summaryActive === "processInfo") {
        this.$nextTick(() => {
          this.$refs.processInfo.refreshChange();

        })
      } else if (this.summaryActive === "reportData") {
        this.$nextTick(() => {
          this.$refs.reportData.refreshChange();
        })
      } else if (this.summaryActive === "bomInfo") {
        this.$nextTick(() => {
          this.$refs.bomInfo.refreshChange();
        })
      } else if (this.summaryActive === "badDetails") {
        this.$nextTick(() => {
          this.$refs.badDetails.refreshChange();
        })
      } else if (this.summaryActive === "scrapDetails") {
        this.$nextTick(() => {
          this.$refs.scrapDetails.refreshChange();
        })
      } else if (this.summaryActive === "checkData") {
        this.$nextTick(() => {
          if (this.processName.includes("测量") && this.measureFlg === "DR8") {
            this.$refs.dr8.searchChange();
          }
          if (this.processName.includes("测量") && this.measureFlg === "GEN") {
            this.$refs.gen.searchChange();
          }
          if (this.processName.includes("干涉")) {
            this.$refs.mtPro.searchChange();
          }
          if (this.processName.includes("极性")) {
            this.$refs.polarity.searchChange();
          }
          if (this.processName.includes("插回损")) {
            this.$refs.opltSHHS.searchChange();
          }
        })
      }
    },
    handleRowClick(row, event) {
      this.processName = row.processName;
      this.docNo = row.docNo;
      this.lineId = row.productLineId;//产线ID
      this.processId = row.processId;//工序ID
      this.workstation = row.workPositionName;//工位
      this.workshopName = row.workshopName;//车间名称
      this.realityLineName = row.productLineName;//实际产线名称
      this.itemCode = row.itemCode;//品号
      //用一个变量结束从this.docNoTypeList中的itemCode判断是否包含this.itemCode,如果包含则判断this.docNoTypeList中的description是否包含DR8
      this.docNoTypeList.forEach(item => {
        var itemCode = item.itemCode;//产线已绑定品号字符串
        if (itemCode.includes(this.itemCode)) {//判断当前产线绑定的品号中是否包含当前行编辑的工单品号 true包含；false}
          if (item.label.includes("DR8")) {
            this.measureFlg = "DR8";
          }
          if (item.label.includes("GEN")) {
            this.measureFlg = "GEN";
          }
        }
      });

      if (this.summaryActive === "processInfo") {
        this.$nextTick(() => {
          this.$refs.processInfo.refreshChange();

        })
      } else if (this.summaryActive === "reportData") {
        this.$nextTick(() => {
          this.$refs.reportData.refreshChange();
        })
      } else if (this.summaryActive === "bomInfo") {
        this.$nextTick(() => {
          this.$refs.bomInfo.refreshChange();
        })
      } else if (this.summaryActive === "badDetails") {
        this.$nextTick(() => {
          this.$refs.badDetails.refreshChange();
        })
      } else if (this.summaryActive === "scrapDetails") {
        this.$nextTick(() => {
          this.$refs.scrapDetails.refreshChange();
        })
      } else if (this.summaryActive === "checkData") {
        this.$nextTick(() => {
          if (this.processName.includes("测量") && this.measureFlg === "DR8") {
            this.$refs.dr8.searchChange();
          }
          if (this.processName.includes("测量") && this.measureFlg === "GEN") {
            this.$refs.gen.searchChange();
          }
          if (this.processName.includes("干涉")) {
            this.$refs.mtPro.searchChange();
          }
          if (this.processName.includes("极性")) {
            this.$refs.polarity.searchChange();
          }
          if (this.processName.includes("插回损")) {
            this.$refs.opltSHHS.searchChange();
          }
        })
      }
    }
  }
}
</script>

<!-- <style scoped>
/deep/ .avue-crud__menu {
  min-height: 0px!important;
}
/deep/.el-card__body, .el-main{
  padding: 0px!important;
}
/deep/.avue-crud__search .el-form-item--small.el-form-item{
  margin-bottom: 0px!important;
}
/deep/.el-tabs__header{
  margin-bottom: 0px!important;
}
</style> -->

<style lang="scss" scoped>
::v-deep .search_self {
    background: #fff;
    padding: 10px;
    margin-bottom: 10px;
}
::v-deep .el-tabs .el-tabs__header {
      margin: 10px 0;
}
.avue_self {
  ::v-deep .basic-container {
    padding: 0;

}
}
</style>
